package view;

import java.awt.Font;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;

import basic.Date;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

import control.Alert;
import control.BudgetManager;

//@author A0098891H
public class SidePane extends JPanel {	

	private final JTextArea ta_notice = new JTextArea();
	private final JTextArea ta_recent = new JTextArea();
	private final JLabel lblRecent = new JLabel("Recent");
	private BudgetManager _budgetMan;
	private Alert _alert;
	
	public SidePane(BudgetManager budgetMan, Alert alert)
	{
		super();
		
		_budgetMan = budgetMan;
		_alert = alert;
		
		
		this.setLayout(new FormLayout(new ColumnSpec[] {
				ColumnSpec.decode("274px"),},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				RowSpec.decode("240px"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				RowSpec.decode("325px"),}));
		JLabel lblNoticeBoard = new JLabel("Notice Board");
		lblNoticeBoard.setFont(new Font("Century Gothic", Font.PLAIN, 18));
		this.add(lblNoticeBoard, "1, 2, center, center");
		ta_notice.setWrapStyleWord(true);
		ta_notice.setFont(new Font("Century Gothic", Font.PLAIN, 15));
		ta_notice.setLineWrap(true);
		ta_notice.setEditable(false);
		ta_notice.setRows(12);
		JScrollPane scroll_notice = new JScrollPane ( ta_notice );
		scroll_notice.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

		this.add(scroll_notice, "1, 3, fill, top");
		lblRecent.setFont(new Font("Century Gothic", Font.PLAIN, 18));

		this.add(lblRecent, "1, 5, center, center");
		ta_recent.setRows(14);
		ta_recent.setWrapStyleWord(true);
		ta_recent.setFont(new Font("Consolas", Font.PLAIN, 15));
		ta_recent.setLineWrap(true);
		ta_recent.setEditable(false);
		ta_recent.setFocusable(true);
		ta_recent.setText(Date.getTimeStamp()+"\n"+"WELCOME!\n");
		JScrollPane scroll_recent = new JScrollPane ( ta_recent );
		scroll_recent.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

		this.add(scroll_recent, "1, 6, fill, fill");
		
		update();

	}
	
	public void appendRecent(String msg)
	{
		ta_recent.append(Date.getHHMMSS());
		ta_recent.append("$ ");
		ta_recent.append(msg);
	}
	
	public void update()
	{
		updateNB();
	}
	
	public void updateNB()
	{
		StringBuilder sb = new StringBuilder();
		sb.append(_budgetMan.printBudgetStatus());
		sb.append("\n");
		sb.append(_alert.getAlertMsg());
		ta_notice.setText(sb.toString());
	}
	
	public void clear()
	{
		ta_recent.setText(Date.getTimeStamp()+"\n");
	}
}
